package com.yunji.cabinet.dao.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yunji.cabinet.bo.CabinetSiteListQueryBO;
import com.yunji.cabinet.dao.ICabinetSiteDao;
import com.yunji.cabinet.domain.CabinetSiteDO;
import com.yunji.cabinet.mapper.ICabinetSiteMapper;
import com.yunji.common.base.BaseDaoImpl;
import com.yunji.common.constant.LogicCode;
import com.yunji.common.utils.bean.BeanUtils;
import org.springframework.stereotype.Repository;

import java.util.Arrays;
import java.util.List;

/**
 * @author : peter-zhu
 * @date : 2024/12/13 11:15
 * @description : TODO
 **/
@Repository
public class CabinetSiteDaoImpl extends BaseDaoImpl<ICabinetSiteMapper, CabinetSiteDO> implements ICabinetSiteDao {

    @Override
    public List<CabinetSiteDO> getCabinetSiteBySid(Long siteId) {
        return lambdaQuery().eq(CabinetSiteDO::getSiteId, siteId).eq(CabinetSiteDO::getStatus, LogicCode.NO).list();
    }

    @Override
    public CabinetSiteDO getCabinetSiteByCid(Long containerId) {
        return lambdaQuery().eq(CabinetSiteDO::getContainerId, containerId).eq(CabinetSiteDO::getStatus, LogicCode.NO).one();
    }

    @Override
    public boolean deleteByContainerIds(Long... ids) {
        return update(new LambdaUpdateWrapper<CabinetSiteDO>()
                .set(CabinetSiteDO::getStatus, LogicCode.YES)
                .eq(CabinetSiteDO::getStatus, LogicCode.NO)
                .in(CabinetSiteDO::getContainerId, Arrays.asList(ids)));
    }

    @Override
    public List<CabinetSiteDO> search(CabinetSiteListQueryBO bo) {
        return list(new LambdaQueryWrapper<CabinetSiteDO>()
                .eq(BeanUtils.isNotNull(bo.getSiteId()), CabinetSiteDO::getSiteId, bo.getSiteId())
                .eq(BeanUtils.isNotNull(bo.getManagerId()), CabinetSiteDO::getManagerId, bo.getManagerId())
                .eq(BeanUtils.isNotNull(bo.getContainerId()), CabinetSiteDO::getContainerId, bo.getContainerId()));
    }

    @Override
    public List<CabinetSiteDO> searchBySiteIds(Long... ids) {
        return list(new LambdaQueryWrapper<CabinetSiteDO>()
                .eq(CabinetSiteDO::getStatus, LogicCode.NO)
                .in(CabinetSiteDO::getSiteId, Arrays.asList(ids)));
    }

}
